﻿using System;
using System.Collections;
using System.Text;
using ZYCommon;

namespace TextEditor
{
    

    public class ZYTextLine
    {
        public ZYTextContainer Container = null;
        public int ContentWidth = 0;
        public ArrayList Elements = new ArrayList();
        public int Height = 0;
        public int Index = 0;
        public int LineSpacing = 0;
        public int RealIndex = 0;
        public int RealLeft = 0;
        public int realTop = 0;
        private int RealRight = 0;
        public int Top = 0;
        public PrintPage ownPage;

        public int RealTop
        {
            get
            {
                return realTop;
            }
            set
            { 
                realTop = value;
            }
        }

        public int CalcuteRealIndex()
        {
            int index = this.Index;
            for (ZYTextContainer container = this.Container; (container != null) && (container.OwnerLine != null); container = container.Parent)
            {
                index += (container.OwnerLine.Index + container.LineSpan) - 1;
            }
            return index;
        }

        public bool isContainerLine()
        {
            foreach (ZYTextElement element in this.Elements)
            {
                if ((element is ZYTextContainer) && !(element is ZYTextBlock))
                {
                    return true;
                }
            }
            return false;
        }

        public string ToZYString()
        {
            if ((this.Elements != null) && (this.Elements.Count > 0))
            {
                StringBuilder builder = new StringBuilder();
                foreach (ZYTextElement element in this.Elements)
                {
                    builder.Append(element.ToZYString());
                }
                return builder.ToString();
            }
            return null;
        }

        public int Bottom
        {
            get
            {
                return (this.Top + this.Height);
            }
        }

        public bool IsSkip
        {
            get { return _isSkip; }
            set { _isSkip = value; }
        }

        private bool _isSkip = false;

        public ZYTextElement FirstElement
        {
            get
            {
                if ((this.Elements != null) && (this.Elements.Count > 0))
                {
                    return (ZYTextElement) this.Elements[0];
                }
                return null;
            }
        }

        public int FullHeight
        {
            get
            {
                return (this.Height + this.LineSpacing);
            }
        }

        public bool HasLineEnd
        {
            get
            {
                return (((this.Elements != null) && (this.Elements.Count > 0)) && ((ZYTextElement) this.Elements[this.Elements.Count - 1]).isNewLine());
            }
        }

        public ZYTextElement LastElement
        {
            get
            {                
                if ((this.Elements != null) && (this.Elements.Count > 0))
                {
                    if (this.Elements.Count > 1 && this.Elements[this.Elements.Count - 1] is ZYTextSplit)
                    {
                        return (ZYTextElement)this.Elements[this.Elements.Count - 2];
                    }
                    return (ZYTextElement)this.Elements[this.Elements.Count - 1];
                }
                return null;
            }
        }

        public int RealBottom
        {
            get
            {
                return (this.RealTop + this.Height);
            }
        }

        public int realRight
        {
            set { RealRight = value; }
        }
    }
}

